/*
 * 添加销售信息
 * 参数   交易码：trancode=15112
 * 
 */

package org.ccams.tran;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TProductplan;

/**
 * 添加销售大区交易
 * 
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 */
public class Tran18112 extends AbstractJsonTran {

	public static final String getAfterDateString(String start_date, int day_i) {
		Calendar day = Calendar.getInstance();
		int year = Integer.parseInt("20" + start_date.substring(0, 2));
		int month = Integer.parseInt(start_date.substring(3, 5)) - 1;
		int date = Integer.parseInt(start_date.substring(6, 8));

		day.set(year, month, date);
		day.add(Calendar.DATE, day_i);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(day.getTime());
	}

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得用户录入信息
		String address_name = (String) request.getParameter("address_name");
		String client_name = (String) request.getParameter("client_name");
		String kind = (String) request.getParameter("kind");
		String lvcai = (String) request.getParameter("lvcai");
		String bolibaiye = (String) request.getParameter("bolibaiye");
		String jiaceng = (String) request.getParameter("jiaceng");
		String number = (String) request.getParameter("number");
		String product = (String) request.getParameter("product");
		String start_date = (String) request.getParameter("start_date");
		String guage = (String) request.getParameter("guage");
		String guage_arrive = (String) request.getParameter("guage_arrive");
		String remark = (String) request.getParameter("remark");

		address_name = (address_name != null) ? address_name : "";
		client_name = (client_name != null) ? client_name : "";
		kind = (kind != null) ? kind : "";
		lvcai = (lvcai != null) ? lvcai : "";
		bolibaiye = (bolibaiye != null) ? bolibaiye : "";
		jiaceng = (jiaceng != null) ? jiaceng : "";
		guage_arrive = (guage_arrive != null) ? guage_arrive : "";
		remark = (remark != null) ? remark : "";

		TProductplan tproductPlan = new TProductplan();
		tproductPlan.setAddress_name(address_name);
		tproductPlan.setClient_name(client_name);
		tproductPlan.setKind(kind);
		tproductPlan.setLvcai(lvcai);
		tproductPlan.setBolibaiye(bolibaiye);
		tproductPlan.setJiaceng(jiaceng);
		tproductPlan.setGuage_arrive(guage_arrive);
		tproductPlan.setRemark(remark);

		if (product == null || product.equals("")) {
			tproductPlan.setProduct(10);
		} else {
			tproductPlan.setProduct(new Integer(product));
		}
		if (start_date == null || start_date.equals("")) {
			start_date = "";
		} else {
			String myDate = "20" + start_date.substring(0, 2) + "-"
					+ start_date.substring(3, 5) + "-"
					+ start_date.substring(6, 8);
			tproductPlan.setStart_date(myDate);
			tproductPlan.setPlan_date(getAfterDateString(start_date,
					tproductPlan.getProduct()));
		}
		if (number == null || number.equals("")) {
			tproductPlan.setNumber(0);
		} else {
			tproductPlan.setNumber(new Integer(number));
		}
		if (guage == null || guage.equals("")) {
			tproductPlan.setGuage(0);
		} else {
			tproductPlan.setGuage(new Integer(guage));
		}

		//调试计划完成日期
		System.out.println("---------------------------");
		System.out.println(tproductPlan.getPlan_date());
		System.out.println("---------------------------");

		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		int i = getCcamsService().getTproductplanDao().insert(tproductPlan);
		if (i == 1) {
			success = true;
			message = "添加成功";
		} else {
			success = false;
			message = "添加失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
